﻿/*
 This file created by Sajad Mehrabi
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace Cs.Multilingual
{

    public class Lang
    {
        #region Filds And Prop
        private string _lang;

        public static readonly string Directory = AppDomain.CurrentDomain.BaseDirectory + "Language\\";

        public string FileName { get; protected set; }

        public bool RightToLeft
        {
            get
            {
                if (!Exists())
                    throw new IOException(string.Format("Language is Not Exists \"{0}\"", FileName));

                var doc = XDocument.Load(FileName);

                return doc.Root != null && doc.Root.Attribute("RightToLeft").Value == true.ToString();

            }
            set
            {
                if (!Exists())
                    throw new IOException(string.Format("Language is Not Exists \"{0}\"", FileName));
                var doc = XDocument.Load(FileName);
                if (doc.Root != null) doc.Root.Attribute("RightToLeft").Value = value.ToString();
                doc.Save(FileName);
            }
        }

        public static List<string> Languages
        {
            get
            {
                return System.IO.Directory.Exists(Directory) ? (from file in System.IO.Directory.GetFiles(Directory) where new Lang(Path.GetFileNameWithoutExtension(file)).Exists() select Path.GetFileNameWithoutExtension(file)).ToList() : new List<string>();
            }
        }

        public string Language
        {
            get { return _lang; }
            set
            {
                _lang = value;
                Properties.Settings.Default.Active = value;
            }
        }

        public IList<string> Ids
        {
            get
            {
                try
                {
                    var doc = XDocument.Load(FileName);
                    IList<string> ret = new List<string>();
                    if (doc.Root != null)
                    {
                        var query = from element in doc.Root.Elements("Text")
                                    select element;

                        ret = query.Select(item => item.Attribute("id").Value).ToList();
                    }
                    return ret;
                }
                catch
                {
                    return new List<string>();
                }
            }
        }
        #endregion

        #region Indexers
        public string this [string index]
        {
            get
            {
                if (!Exists())
                    throw new IOException(string.Format("Language is Not Exists \"{0}\"", FileName));

                var doc = XDocument.Load(FileName);

                if (doc.Root != null)
                {
                    try
                    {
                        var query = (from text in doc.Root.Elements("Text")
                                     where text.Attribute("id").Value == index
                                     select text).Single();

                        return query.Value;
                    }
                    catch
                    {
                        return string.Empty;
                    }
                }

                return string.Empty;
            }
            set
            {
                if (!Exists())
                    throw new IOException(string.Format("Language is Not Exists \"{0}\"", FileName));

                var doc = XDocument.Load(FileName);

                if (doc.Root == null) return;

                var query = from element in doc.Root.Elements("Text")
                            where element.Attribute("id").Value ==  index
                            select element;

                var elements = query as IList<XElement> ?? query.ToList();
                if (elements.Any())
                {
                    var elm = elements.First();
                    if (string.IsNullOrEmpty(value))
                        elm.Remove();
                    else
                        elm.Value = value;
                    doc.Save(FileName);
                }
                else
                {
                    var elm = doc.Root;
                    elm.Add(new XElement("Text", new XAttribute("id",  index), value));

                    doc.Save(FileName);
                }
            }
        }

        public string this [int index] 
        { 
            get { return this[string.Format("{0:X}", index)]; } 
            set { this[string.Format("{0:X}", index)] = value; }
        }
        #endregion

        #region Methods

        public static void Editor(bool dialog = false)
        {
            var editor = new Editor();
            if (dialog)
                editor.ShowDialog();
            else
                editor.Show();
        }

        public Lang(string useLang)
        {
            Language = useLang;
            FileName = string.Format("{0}{1}.xml", Directory, Language);
        }

        public void CreateLanguage()
        {
            if (Exists())
                throw new IOException(string.Format("Language is Exists \"{0}\"", FileName));

            if (File.Exists(FileName))
            {
                var i = 1;
                while (File.Exists(string.Format(FileName + "{0}", i)))
                {
                    i++;
                }
                File.Move(FileName, string.Format(FileName + "{0}", i));
            }

            if (!System.IO.Directory.Exists(Directory))
                System.IO.Directory.CreateDirectory(Directory);

            var xmldoc = new XmlDocument();
            var el = (XmlElement) xmldoc.AppendChild(xmldoc.CreateElement("Language"));
            el.SetAttribute("id", _lang);
            el.SetAttribute("RightToLeft", false.ToString());
            xmldoc.Save(FileName);

        }

        public bool Exists()
        {
            if (!File.Exists(FileName)) return false;

            var doc = XDocument.Load(FileName);
            if (doc.Root == null) return false;

            return doc.Root.Name == "Language";
        }





        #endregion
    }
}
